package com.rookieci.totp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.rookieci.totp.activity.QrCodeListActivity;
import com.rookieci.totp.activity.QrCodeTextActivity;
import com.rookieci.totp.db.QRCodeDB;
import com.rookieci.totp.mode.TotpMode;
import com.rookieci.totp.utils.TotpUtils;

import java.util.List;

import io.github.xudaojie.qrcodelib.CaptureActivity;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_bottom_menu, menu);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        if (item.getItemId() == R.id.navigation_scan1) {
            CaptureActivity.open(this, 101, 1);
        } else if (item.getItemId() == R.id.navigation_scan2) {
            CaptureActivity.open(this, 102, 2);
        } else if (item.getItemId() == R.id.navigation_list) {
            QrCodeListActivity.open(this);
            /*TODO 此处测试 Clock clock = new Clock(30);

            String secret = "EAOFFAEYBJBT5B5YWRFZYGBNQG7PUW2T";
            Totp totp = new Totp(secret, clock);
            String otpCode = totp.now();//动态令牌

            long time = 30 - System.currentTimeMillis() / 1000 % 30;

            new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("otpCode：" + otpCode + "\ntime:" + time)
                    .setPositiveButton("确定", null)
                    .show();*/
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 101) {//条形码
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                if (bundle == null) return;
                String codeStr = bundle.getString("result");
                if (TextUtils.isEmpty(codeStr)) return;
                openQRCode(codeStr, 1);
            }
        } else if (requestCode == 102) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                if (bundle == null) return;
                String codeStr = bundle.getString("result");
                if (TextUtils.isEmpty(codeStr)) return;
                openQRCode(codeStr, 2);
            }
        } else super.onActivityResult(requestCode, resultCode, data);
    }

    private void openQRCode(String codeStr, int type) {
        //保存
        QRCodeDB qrCodeDB = new QRCodeDB();
        qrCodeDB.insert(codeStr, String.valueOf(type));
        //判断是不是JSON
        List<TotpMode> list = TotpUtils.getModeOfJson(codeStr);
        if (list != null && list.size() > 0) {
            //TODO 解析成功
        } else {
            TotpMode totpMode = TotpUtils.getModeOfScheme(codeStr);
            if (totpMode != null) {//TODO 解析成功
            }
        }
        //TODO 判断是不是 otpauth:// 开头
        //跳详情页
        QrCodeTextActivity.open(this, codeStr, true);
    }

    //[{"algorithm":"SHA1","digits":6,"issuer":"fnOS","period":30,"secret":"EAOFFAEYBJBT5B5YWRFZYGBNQG7PUW2T"
// ,"type":"totp","username":"rookieCi@rookieCiNas"},{"algorithm":"SHA1","digits":6,"issuer":"fnOS","period":30,"secret":"TUNQSWDNCJWELKAALGSXDQIK4KPFULHG","type":"totp","username":"rookieCi@rookieCiNas"}]

    //type / (issuer) : username ?
//otpauth://totp/%E4%B8%80%E6%9E%9D%E6%A2%85%E7%9A%84%E5%8D%9A%E5%AE%A2:kdyzm?secret=EN6ABQIJUYH5EABTKDVXOZWMGITH664C
//&issuer=%E4%B8%80%E6%9E%9D%E6%A2%85%E7%9A%84%E5%8D%9A%E5%AE%A2&algorithm=SHA1&digits=6&period=30

}